﻿using FB.Membership;
using FB.Membership.Principal;
using FB.Providers;
using FB.Providers.EmployeeInfo;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mvc;


namespace FB.Mvc.Controllers
{
    public abstract class BaseController : Controller
    {
        #region  craate by canding by  2013-08-15 for Foa
        public   Guid UserID
        {
            get
            {
               
              return  FBIdentity.UserId ;
            }
        }

        public   string UserName
        {
            get
            {
                if (FBPrincipal == null)
                    return string.Empty;
                if (FBPrincipal.EmployeeInfo != null && !string.IsNullOrEmpty(FBPrincipal.EmployeeInfo.ChineseName))
                {
                    return FBPrincipal.EmployeeInfo.ChineseName;
                }else
                {
                    return FBIdentity.Name;
                }
            }
        }

        //public IEnumerable<FB.Data.Models.Department> Deparment
        //{
        //    get { return FBPrincipal.EmployeeInfo.Departments; }
        //}

        public string DeparmentName
        {
            get
            {
                if (FBPrincipal == null)
                    return string.Empty;

                //return "财务部";
                if (null != FBPrincipal.EmployeeInfo.PrimaryPosition)
                    return FBPrincipal.EmployeeInfo.PrimaryPosition.DEPTName;
                else
                    return string.Empty;
              
            }
        }
        public string DeparmentID
        {
            get
            {
                if (FBPrincipal == null)
                    return string.Empty;

                //return "D2";
                if (null != FBPrincipal.EmployeeInfo.PrimaryPosition)
                    return FBPrincipal.EmployeeInfo.PrimaryPosition.DEPTCode;
                else
                    return string.Empty;
            }
        }

        protected Employee GetEmployeeByUserKey(Guid userID)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit();
            Employee employee = unitinfo.EmployeeRepository.Get(t => t.UserID == userID).FirstOrDefault();
            if (employee == null)
            {
                employee = new Employee()
                {
                    ChineseName = "信息缺失用户",
                };
            }
            return employee; 
        }
        protected Employee GetEmployeeByUserName(string userName)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit();
            Employee employee = unitinfo.EmployeeRepository.Get(t => t.UserName == userName&&t.UserID!=null).FirstOrDefault();
            if (employee == null)
            {
                employee = new Employee()
                {
                    ChineseName = "信息缺失用户",
                };
            }
            return employee;
        }
        protected FB.Providers.EmployeeInfo.Department GetDepartment(string DEPTCode)
        {
            var efemployeeInfoProvider = AspNetEmployeeInfoProviderManager.Default as EFEmployeeInfoProvider;
            EmployeeInfoUnit unitinfo = efemployeeInfoProvider.GetEmployeeInfoUnit();
            FB.Providers.EmployeeInfo.Department dept = unitinfo.DepartmentRepository.Get(t => t.DEPTCode == DEPTCode).FirstOrDefault();
            if (dept == null)
            {
                dept = new Department()
                {
                      Name="缺乏部门"
                };
            }
            return dept;

        }
        #endregion 
        public FBIdentity FBIdentity
        {
            get
            {
                if (User.Identity is FBIdentity)
                {
                    return (FBIdentity)User.Identity;
                }
                else
                {
                    return null;
                }
            }
        }

        public FBPrincipal FBPrincipal
        {
            get
            {
                return (User is FBPrincipal) ? (FBPrincipal)User : null;
            }
        }

        public FBProfile FBProfile { get { return base.Profile as FBProfile; } }

        public bool CanMockUser { set { Session["CanMockUser"] = value; } get { return Session["CanMockUser"] != null ? (bool)Session["CanMockUser"] : false; } }

        public BaseController()
        {
            //Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("zh-CHT");
            //Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CHT");
        }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            // update user count
            if (null != User && HttpContext.User.Identity.IsAuthenticated)
            {
                FB.Membership.AspNetMembershipProviderWrapper m = new AspNetMembershipProviderWrapper();
                m.Touch(User.Identity.Name);
            }
        }


        protected string GetErrorMessageForModel()
        {
            var ErrMsg = string.Empty;
            if (!this.ModelState.IsValid)
            {
                foreach (var key in this.ModelState.Keys)
                {
                    foreach (var err in this.ModelState[key].Errors)
                    {
                        ErrMsg += string.Format(" - {0}<br/>", err.ErrorMessage) + Environment.NewLine;
                    }
                }
            }

            return ErrMsg;
        }
    }
}